//
//  AnchorViewController.m
//  网络FM
//
//  Created by dlios on 15/5/23.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//

#import "AnchorViewController.h"
#import "FeelingTableViewCell.h"
#import "AnchorView.h"

#import "SearchAnchorDetailModel.h"

#import <AFNetworking.h>


#import <UIImageView+WebCache.h>


#import "PlayerViewController.h"

@interface AnchorViewController ()

@end

@implementation AnchorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//[self createTableView];

 //   [self SearchConnetion];

}

- (void)loadView
{
    [super loadView];
    [self SearchConnetion];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc
{
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
 
    }
    return self;
}





- (void)createTableView
{
    AnchorView *anchor = [[AnchorView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)];
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    //给anchor赋值
    anchor.anchorName.text = self.model.username;
    [anchor.anchorName setFont:[UIFont systemFontOfSize:15]];
    anchor.anchorView.clipsToBounds = YES;
    anchor.anchorView.layer.cornerRadius = 40;
    [anchor setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Anchor_backImage.jpg"]]];
  [anchor.anchorView sd_setImageWithURL:[NSURL URLWithString:self.model.avatar] placeholderImage:nil options:SDWebImageRetryFailed];

    
    [self.table  setTableHeaderView:anchor];
    self.table.rowHeight = self.view.frame.size.width / 4 + 20;
    [self.table setContentSize:CGSizeMake(0, 180 + self.table.rowHeight * self.model.fmnum)];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table release];
 
 
    
    
}


//UITableView两个必须实现的协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.searchAncherDetailArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellinentify = @"cell";
    FeelingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellinentify];
    if (!cell) {
        cell = [[FeelingTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellinentify];
    }
    
 
    
    SearchAnchorDetailModel *detail = [self.searchAncherDetailArray objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:detail.cover] placeholderImage:nil options:SDWebImageRetryFailed];
    cell.FMName.text = detail.title;

    cell.anchorName.text = [NSString stringWithFormat:@"主播  %@",detail.speak];
    cell.listen_count.text = [NSString stringWithFormat:@"收藏  %ld",(long)detail.favnum];

    return cell;
    
}





//主播详细界面下的网络请求
- (void)SearchConnetion
{
    self.searchAncherDetailArray = [NSMutableArray array];

    //anchorDetailcell的网络请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://bapi.xinli001.com/fm2/broadcast_list.json/?speaker_id="];
    NSString *s = [NSString stringWithFormat:@"%@%@",str,self.model.ID];
    [manager GET:s parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",bigDic);
        NSMutableArray *array = [bigDic objectForKey:@"data"];
        for (NSMutableDictionary *d in array) {
            SearchAnchorDetailModel *model = [[SearchAnchorDetailModel alloc ]init];
            [model setValuesForKeysWithDictionary:d];
            [self.searchAncherDetailArray addObject:model];
        }
        [self createTableView];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
   
    
}



//实现跳转到播放界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerViewController *player = [[PlayerViewController alloc]init];
    
    
    SearchAnchorDetailModel *model = [self.searchAncherDetailArray objectAtIndex:indexPath.row];
    
    player.urlForPlay = model.url;
    
    [self presentViewController:player animated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
